var mongoose = require('mongoose');

// 使用bluebird作为mongoose的promise库
mongoose.Promise = require('bluebird');

console.log('[Allspark].[INFO].process.env.NODE_ENV = ' + process.env.NODE_ENV);
var dbIp = (typeof(process.env.NODE_ENV) === 'undefined' ||
    process.env.NODE_ENV == 'production') ? process.env.MONGODB_PORT_27017_TCP_ADDR : '127.0.0.1';
var dbname = 'co-research';
if (process.env.NODE_ENV == 'test') {
  dbname = 'co-research-test'
} else if (process.env.NODE_ENV == 'development') {
  dbname = 'co-research-dev'
}

mongoose.connect( "mongodb://" + dbIp + "/" + dbname, function(err) {
  if (err) {
    console.info('>>>>>>>>>> [allspark].[error].db connect error');
    console.info(err);
  }
});

module.exports.mongoose = mongoose;